﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PdfLibrary
{
    public class PdfVector
    {
        public double X, Y;

        public PdfVector(double x, double y)
        {
            X = x;
            Y = y;
        }
    }

    public class TransformationMatrix
    {
        public double[,] Values;

        public TransformationMatrix()
        {
            Values = new[,] { { 1.0, 0.0 }, { 0.0, 1.0 }, { 0.0, 0.0 } };
        }

        public TransformationMatrix(params double[] values)
        {
            Values = new[,] { { values[0], values[1] }, { values[2], values[3] }, { values[4], values[5] } };
        }

        public static TransformationMatrix operator *(TransformationMatrix matrixA, TransformationMatrix matrixB)
        {
            var a = matrixA.Values;
            var b = matrixB.Values;
            return new TransformationMatrix(new[]{ 
                a[0, 0] * b[0, 0] + a[0, 1] * b[1, 0],
                a[0, 0] * b[0, 1] + a[0, 1] * b[1, 1],
                a[1, 0] * b[0, 0] + a[1, 1] * b[1, 0],
                a[1, 0] * b[0, 1] + a[1, 1] * b[1, 1],
                a[2, 0] * b[0, 0] + a[2, 1] * b[1, 0] + b[2, 0],
                a[2, 0] * b[0, 1] + a[2, 1] * b[1, 1] + b[2, 1],
            });
        }

        public TransformationMatrix Translate(double tx, double ty)
        {
            return GetTranslateMatrix(tx, ty) * this;
        }

        public TransformationMatrix GetTranslateMatrix(double tx, double ty)
        {
            return new TransformationMatrix(1, 0, 0, 1, tx, ty);
        }

        public PdfVector TransformAsVector(PdfVector v)
        {
            return new PdfVector(
                v.X * Values[0, 0] + v.Y * Values[1, 0],
                v.X * Values[0, 1] + v.Y * Values[1, 1]);
        }

        public TransformationMatrix Clone()
        {
            return new TransformationMatrix() { Values = (double[,])Values.Clone() };
        }
    }
}
